<?php


namespace app\common\model\order;


use think\Model;

class OrderExpress extends Model
{
    protected $name = 'order_express';


    /**
     * 获取订单物流包裹信息
     *
     * @param int $order_id 订单ID
     */
    public function getExpressList($params, $user_id): array
    {
        extract($params);

        try {
            $list = $this
                ->field('id as order_express_id,user_id,order_id,express_name,express_no')
                ->where(['order_id' => $order_id])
                ->select();
            if(!$list){
                throw new \Exception('未找到包裹信息',45401);
            }
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => $list
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => $e->getLine()
            ];
        }
    }

    /**
     * 查询物流信息
     *
     * @param int $order_express_id 包裹ID
     */
    public function express($params, $user_id)
    {
        extract($params);

        try {
            $data = $this->get($order_express_id);
            if(!$data){
                throw new \Exception('未找到包裹信息',45401);
            }
            $order = model('app\common\model\order\OrderModel')->get($data->order_id);
            // 获取快递
            switch ($order['status']) {
                case 0:
                    $express[] = [
                        'context' => '付款后，即可将宝贝发出',
                        'status' => '尚未付款',
                        'time' => date('Y-m-d H:i:s', $order['createtime'])
                    ];
                    break;
                case 1:
                    $express[] = [
                        'context' => '商家接受到您的订单，准备出库',
                        'status' => '已下单',
                        'time' => date('Y-m-d H:i:s', $order['pay_time'])
                    ];
                    break;
                default: // 获取物流
                    $KuaidiSubModel = model('app\common\model\KuaidiSubModel')
                        ->where(['express_no' => $data['express_no']])
                        ->find();
                    if($KuaidiSubModel){
                        $express = json_decode($KuaidiSubModel['data'], true);
                        if(!$express) {
                            $getExpress = (new \app\common\library\Express)->getLogistics($data['express_code'],$data['express_no']);
                            if($getExpress['code'] == 1) {
                                $getExpressData = $getExpress['data'];
                                $KuaidiSubModel->message = $getExpressData['message'];
                                $KuaidiSubModel->status = $getExpressData['status'];
                                $KuaidiSubModel->state = $getExpressData['state'];
                                $KuaidiSubModel->ischeck = $getExpressData['ischeck'];
                                $KuaidiSubModel->com = $getExpressData['com'];
                                $KuaidiSubModel->data = json_encode($getExpressData['data']);
                                $KuaidiSubModel->save();
                                $express = $getExpressData['data'];
                            }
                        }
                    }else{
                        $express[] = [
                            'context' => '打包完成，正在等待快递小哥揽收~',
                            'status' => '仓库处理中',
                            'time' => date('Y-m-d H:i:s', $order['delivery_time'])
                        ];
                    }
            }
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => $express
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => $e->getLine()
            ];
        }
    }

}